<?php
namespace Core\Controller;

use Core\Exception\BusinessException;
use Core\Service\Authenticate\IdentityManager;
use Core\Service\Authenticate\IdentityManagerInterface;
use Core\Session\AdminSessionHelper;
use Core\Session\ApplicationSessionHelper;
use Model\TableFinder;
use Model\User;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

class AdminBaseController extends  BaseController{

    /**
     * @var User
     */
    protected $_user;

    /**
     * @var IdentityManagerInterface;
     */
    protected $identityManager;



    public function __construct(IdentityManagerInterface $identityManager)
    {
        $this->identityManager = $identityManager;
    }



    public function onDispatch(MvcEvent $e)
    {
        $this->layout('admin_layout.php');
        $user = $this->getUser();
        if(!$user){
            $identityManager = $this->getIdentityManager();
            $userInSession = $identityManager->getIdentity();
            $this->_user = $userInSession;
            if(!$userInSession)
                return $this->redirect()->toRoute('admin/login', array());


        }

        $controller = $this->getEvent()->getRouteMatch()->getParam('controller');
        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        ApplicationSessionHelper::setCurrentModule('admin');
        ApplicationSessionHelper::setCurrentController($controller);
        ApplicationSessionHelper::setCurrentAction($action);

        return parent::onDispatch($e);
    }



    /**
     * @param \Core\Service\Authenticate\IdentityManagerInterface $identityManager
     */
    public function setIdentityManager($identityManager)
    {
        $this->identityManager = $identityManager;
    }

    /**
     * @return \Core\Service\Authenticate\IdentityManagerInterface
     */
    public function getIdentityManager()
    {
        return $this->identityManager;
    }


    /**
     * @param \Model\User $user
     */
    public function setUser($user)
    {
        $this->_user = $user;
    }

    /**
     * @return \Model\User
     */
    public function getUser()
    {
        return $this->_user;
    }

    protected function genJsonErrorView(\Exception $ex){
        if($ex instanceof BusinessException){
            $msg = $ex->getMessage();
        }else{
            $msg = 'Lỗi hệ thống. Vui lòng liên hệ chúng tôi để khắc phục.';
        }
        return new  JsonModel(array('success'=>false, 'msg'=>$msg));
    }


    protected function genErrorView(\Exception $ex){
        if($ex instanceof BusinessException){
            $msg = $ex->getMessage();
        }else{
            $msg = 'Lỗi hệ thống. Vui lòng liên hệ chúng tôi để khắc phục.';
        }
        $viewModel = new  ViewModel(array('error_msg' => $msg));
        $viewModel->setTerminal(true);
        return $viewModel;
    }



}
